package com.aistar.controller;

import com.aistar.pojo.Choice;
import com.aistar.pojo.Customer;
import com.aistar.pojo.QuestionDetail;
import com.aistar.pojo.QuestionSet;
import com.aistar.service.ChoiceService;
import com.aistar.service.QuestionDetailService;
import com.aistar.service.QuestionSetService;
import com.aistar.util.MessageUtil;
import com.aistar.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
public class ChoiceController {

    @Autowired
    private ChoiceService choiceService;
    @Autowired
    private QuestionSetService questionSetService;
    @Autowired
    private QuestionDetailService questionDetailService;

    @RequestMapping("/choose")
    public String choose(){
        return "choose/chooseType";
    }

    @RequestMapping("/choose/answer")
    public String chooseResult(){
        return "choose/chooseResultAnalysis";
    }


    //跳转到选择题的详情页面
    @GetMapping("/choose/toDetail/{kw}")
    public ModelAndView toDetail(HttpServletRequest request , @PathVariable("kw") String kw){
        ModelAndView mav=new ModelAndView();

        //1.根据关键字获得题目
        String[] key=new String[0];
        if(!kw.equals("all"))
            key = kw.split(",");
        ServerResponse serverResponse = choiceService.getByKw(key);

        //2.获得用户的Id
        Customer customer = (Customer) request.getSession().getAttribute("loginCustomer");
        Integer custId = customer.getCustomerId();
        System.out.println("登录用户id:" + custId);

        //3.根据custId获得QuestionSet，添加questionSet对象，
        QuestionSet questionSet=new QuestionSet();
        questionSet.setCustomerId(custId);
        questionSet.setOther1(kw + "专项练习");
        questionSetService.add(questionSet);

        if(questionSetService.getLatest(custId).getStatus()==1)
            questionSet=(QuestionSet)questionSetService.getLatest(custId).getData();

        //4.根据题目的id和 QuestionSet的id生成 10个questionDetail对象，并添加到数据库中
        List<Choice> choiceList=(List<Choice>) serverResponse.getData();

        for (Choice choice:choiceList) {
            QuestionDetail questionDetail=new QuestionDetail();
            Integer choiceId=choice.getChoiceId();
            questionDetail.setChoiceId(choiceId);
            questionDetail.setQuestionSetId(questionSet.getQuestionSetId());
            questionDetailService.add(questionDetail);
        }

        serverResponse=questionDetailService.getByQuestionSetId(questionSet.getQuestionSetId());
        List<QuestionDetail> questionDetailList=(List<QuestionDetail>)serverResponse.getData();

        String viewName="redirect:/choose/chooseDetail/"+questionSet.getQuestionSetId()+"/"+questionDetailList.get(0).getChoiceId();

        if (serverResponse.getStatus()==0)
            viewName="choose/chooseType";
        mav.setViewName(viewName);
        return mav;
    }
    @GetMapping("/choose/toDetail")
    public ModelAndView toDetail(){
        ModelAndView mav=new ModelAndView();
        mav.addObject("serverResponse","未选择条件,请选择标签");
        mav.setViewName("choose/chooseType");
        return mav;
    }


}
